#!/bin/sh

arcan_base=${HOME}/.arcan
applname="$(basename $0)"
distargs=""

if [ ! -z "${XDG_DATA_HOME}" ]; then
	arcan_base=${XDG_DATA_HOME}/arcan
	distargs="-d ${HOME}/.config/arcan.sqlite"
fi

if [ ! -z "${XDG_CONFIG_HOME}" ]; then
	distargs="-d ${XDG_CONFIG_HOME}/arcan.sqlite"
fi

arcan_applbase=$arcan_base/appl
arcan_applout=$arcan_base/appl-out
arcan_resources=$arcan_base/resources
arcan_logpath=$arcan_base/logs

# a dev would've overridden this one
if [ ! -d "$arcan_applbase/$applname" ]; then
	mkdir -p "$arcan_applbase"
	ln -s "/usr/share/$applname/$applname" "$arcan_applbase/$applname"
fi

if [ ! -d "$arcan_applout/$applname" ]; then
	mkdir -p "$arcan_applout/$applname"
fi

# read-only resource access for browser, note that the presence
# of fonts and logs in this folder will define the font and logpaths
if [ ! -d "$arcan_resources" ]; then
	mkdir -p "$arcan_resources"

# this will automatically set the ARCAN_STATEBASEPATH used for application
# state snapeshots (if supported)
	mkdir -p "$arcan_resources/savestates"
	ln -s "$HOME" "$arcan_resources/home"
fi

# if the user expected the XDG set of folders, map those into our space
if [ ! -z "${XDG_DESKTOP_DIR}" ] && [ ! -d "${arcan_resources}/Desktop" ]; then
	ln -s "${XDG_DESKTOP_DIR}" "${arcan_resources}/Desktop"
fi

if [ ! -z "${XDG_DOCUMENTS_DIR}" ] && [ ! -d "${arcan_resources}/Documents" ]; then
	ln -s "${XDG_DOCUMENTS_DIR}" "${arcan_resources}/Documents"
fi

if [ ! -z "${XDG_DOWNLOADS_DIR}" ] && [ ! -d "${arcan_resources}/Downloadss" ]; then
	ln -s "${XDG_DOWNLOADS_DIR}" "${arcan_resources}/Downloads"
fi

if [ ! -z "${XDG_MUSIC_DIR}" ] && [ ! -d "${arcan_resources}/Music" ]; then
	ln -s "${XDG_MUSIC_DIR}" "${arcan_resources}/Music"
fi

if [ ! -z "${XDG_PICTURES_DIR}" ] && [ ! -d "${arcan_resources}/Pictures" ]; then
	ln -s "${XDG_PICTURES_DIR}" "${arcan_resources}/Pictures"
fi

if [ ! -z "${XDG_PUBLICSHARE_DIR}" ] && [ ! -d "${arcan_resources}/Public" ]; then
	ln -s "${XDG_PUBLICSHARE_DIR}" "${arcan_resources}/Public"
fi
if [ ! -z "${XDG_TEMPLATES_DIR}" ] && [ ! -d "${arcan_resources}/Templates" ]; then
	ln -s "${XDG_TEMPLATES_DIR}" "${arcan_resources}/Templates"
fi

if [ ! -z "${XDG_VIDEOS_DIR}" ] && [ ! -d "${arcan_resources}/Videos" ]; then
	ln -s "${XDG_VIDEOS_DIR}" "${arcan_resources}/Videos"
fi

# this is opt-in, user must make this directory himself as it can get quite verbose
if [ -d "${arcan_logpath}" ]; then
	export ARCAN_LOGPATH=${1:-$arcan_logpath}
fi

# setup namespace mapping
export ARCAN_APPLBASEPATH=${1:-$arcan_applbase}
export ARCAN_APPLSTOREPATH=${1:-$arcan_applout}
export ARCAN_RESOURCEPATH=${1:-$arcan_resources}

# local writable custom / saved scripts that should shadow the builtin ones
overlay=$ARCAN_APPLSTOREPATH/$applname
if [ ! -d "$overlay/output" ]; then
	overrides="output ipc debug devmaps/display devmaps/keyboard devmaps/game devmaps/led devmaps/schemes devmaps/touch tools widgets"
	for i in $overrides; do
		mkdir -p "$overlay/$i"
	done
fi

# there are more we could do to make this easier to work with, i.e. check exit code,
# on failure, try and unmap the RWOUT and add a message that they were discarded due
# to scripting errors, along with capturing and forwarding crash reasons (not full
# snap due to the possibility of sensitive data)
while true; do
	starttime=$(date +%s)

	if [ -d "${arcan_logpath}" ]; then
		if arcan ${distargs} -b "$applname" "$applname" "$@" >"${arcan_logpath}/${applname}_${starttime}.log" 2>&1; then
			exit
		fi
	else
		if arcan ${distargs} -b "$applname" "$applname" "$@"; then
			exit
		fi
	fi

	stoptime=$(date +%s)
	elapsed=$((stoptime - starttime))
	if [ $elapsed -lt 10 ]; then
		exit
	fi
done
